package com.cardmanager.pro.executor;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.cardmanager.pro.util.RestTemplateUtil;

import net.sf.json.JSONObject;
@Component
public class BaseExecutor {

	@Autowired
	private RestTemplateUtil util;

	private final Logger logger = LoggerFactory.getLogger("BaseExecutor");

	/**
	 * 生成订单
	 * @param userId
	 * @param rate
	 * @param type
	 * @param amount
	 * @param realAmount
	 * @param creditCardNumber
	 * @param channelTag
	 * @param orderCode
	 * @param serviceCharge
	 * @param description
	 * @param remark
	 * @return
	 * @throws RuntimeException
	 */
	public JSONObject addCreditCardOrder(
			String userId, String rate, String type, String amount, String realAmount,
			String creditCardNumber, String channelTag, String orderCode, String serviceCharge, String description,
			String remark) throws RuntimeException{
		RestTemplate restTemplate = new RestTemplate();
		URI uri = util.getServiceUrl("transactionclear", "error url request");
		String url = uri.toString() + "/v1.0/transactionclear/payment/add/credit/card/manager/order";
		LinkedMultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String, String>();
		requestEntity.add("userId", userId);
		requestEntity.add("rate",rate );
		requestEntity.add("type", type);
		requestEntity.add("amount",amount );
		requestEntity.add("realAmount",realAmount );
		requestEntity.add("channelTag",channelTag );
		requestEntity.add("orderCode",orderCode );
		requestEntity.add("serviceCharge", serviceCharge);
		requestEntity.add("description", description);
		requestEntity.add("remark", remark);
		requestEntity.add("creditCardNumber", creditCardNumber);
		JSONObject resultJSONObject;
		System.out.println(requestEntity);
		try {
			String resultString = restTemplate.postForObject(url, requestEntity, String.class);
			resultJSONObject = JSONObject.fromObject(resultString);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return resultJSONObject;
	}


	public JSONObject paymentTopupRequest(
			String amount,String ordercode,String orderdesc,String userid,String brandcode,
			String channel_type,String channel_tag
			) throws RuntimeException{
		RestTemplate restTemplate = new RestTemplate();
		URI uri = util.getServiceUrl("paymentchannel", "error url request");
		String url = uri.toString() + "/v1.0/paymentchannel/freeQuick/repayment";
		LinkedMultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String, String>();
		requestEntity.add("userid", userid);
		requestEntity.add("brandcode",brandcode );
		requestEntity.add("channel_type", channel_type);
		requestEntity.add("amount",amount );
		requestEntity.add("channel_tag",channel_tag );
		requestEntity.add("ordercode",ordercode );
		requestEntity.add("orderdesc", orderdesc);
		JSONObject resultJSONObject;
		try {
			String resultString = restTemplate.postForObject(url, requestEntity, String.class);
			resultJSONObject = JSONObject.fromObject(resultString);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return resultJSONObject;
	}
	
	public JSONObject getUserInfo(String userId) throws RuntimeException{
		RestTemplate restTemplate = new RestTemplate();
		URI uri = util.getServiceUrl("user", "error url request");
		String url = uri.toString() + "/v1.0/user/find/by/userid";
		LinkedMultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String, String>();
		requestEntity.add("userId", userId);
		JSONObject resultJSONObject;
		try {
			String resultString = restTemplate.postForObject(url, requestEntity, String.class);
			resultJSONObject = JSONObject.fromObject(resultString);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return resultJSONObject;
		
	}

	/**
	 * 执行交易计划
	 * @param amount
	 * @param userId
	 * @param bankAccountNo
	 * @return
	 * @throws RuntimeException
	 */
	public Map paymentConsumptionTask(String amount,String userId,String bankAccountNo,String inAmount,String rate,String channelId,String channelTag,String orderNo) throws RuntimeException{
		Map<String,String> map = new HashMap();
		try {
			RestTemplate restTemplate = new RestTemplate();
			URI uri = util.getServiceUrl("paymentchannel", "error url request");
			String url = uri.toString() + "/v1.0/paymentchannel/freeQuick/consumptionTask";
			LinkedMultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String, String>();
			requestEntity.add("bankCardNo",bankAccountNo );//银行账号
			requestEntity.add("userId", userId);//用户id
			requestEntity.add("amount", amount);//金额
			requestEntity.add("inAmount", inAmount);//输入金额
			requestEntity.add("rate", rate);//用户费率
			requestEntity.add("orderNo",orderNo);
			requestEntity.add("channelProductCode", channelId);//通道id
			requestEntity.add("channelTag", channelTag);//通道类型

			String result = restTemplate.postForObject(url, requestEntity, String.class);
			map = JSONObject.fromObject(result);
			return map;
		} catch (Exception e) {
			logger.error("paymentConsumptionTask,执行代还任务，回调接口异常{} " + e.getMessage());
			throw new RuntimeException(e);
		}

	}

	/**
	 * 执行还款计划
	 * @param amount
	 * @param userId
	 * @param bankAccountNo
	 * @return
	 * @throws RuntimeException
	 */
	public Map paymentRepaymentTask(String amount,String userId,String bankAccountNo,String channelId,String rate,String inAmount,String channelTag) throws RuntimeException{
		Map<String, String> map = new HashMap();
		try {
			RestTemplate restTemplate = new RestTemplate();
			URI uri = util.getServiceUrl("paymentchannel", "error url request");
			String url = uri.toString() + "/v1.0/paymentchannel/freeQuickPay/taskWithdraw";
			LinkedMultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<>();
			requestEntity.add("bankAccountNo",bankAccountNo );//银行账号
			requestEntity.add("userId", userId);//用户id
			requestEntity.add("amount", amount);//金额
			requestEntity.add("inAmount", inAmount);//输入金额
			requestEntity.add("rate", rate);//用户费率
			requestEntity.add("channelCode", channelId);//通道id
			requestEntity.add("channelTag", channelTag);//通道类型
			String result = restTemplate.postForObject(url, requestEntity, String.class);
			map = JSONObject.fromObject(result);
			return map;
		} catch (Exception e) {
			logger.error(" paymentRepaymentTask,执行代还任务，回调接口异常{} " + e.getMessage());
			throw new RuntimeException(e);
		}

	}

	/**
	 * 执行还款计划
	 * @return
	 * @throws RuntimeException
	 */
	public Map querySyn(String orderNo) throws RuntimeException{
		Map<String, String> map = new HashMap();
		try {
			RestTemplate restTemplate = new RestTemplate();
			URI uri = util.getServiceUrl("paymentchannel", "error url request");
			String url = uri.toString() + "/v1.0/paymentchannel/freeQuickPay/query/syn";
			LinkedMultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<>();
			requestEntity.add("requestNo",orderNo );//订单号
			String result = restTemplate.postForObject(url, requestEntity, String.class);
			map = JSONObject.fromObject(result);
			return map;
		} catch (Exception e) {
			logger.error(" paymentRepaymentTask,执行代还任务，回调接口异常{} " + e.getMessage());
			throw new RuntimeException(e);
		}

	}


}
